set more off

use dataset, clear

local covariates pctwhite pct65plus pcths lmedianincome logvap2014 pctui2013

local Z pcteligible2013
su `Z', det
gen Z = `Z' >=r(p50)
replace Z = . if mi(`Z')
 
gen Zx = Z*x
gen Zz = Z*z
gen Zzx = Z*zx

foreach x in z Z Zz {
	gen `x'B = ""
	gen `x'CIL = ""
	gen `x'CIU = ""	
	gen `x'CI = ""
}
foreach x in z Z Zz {
	gen `x'N = ""
	gen `x'Ns = ""
	gen `x'bw = ""
	gen `x'cov = ""
	gen `x'rsq = ""
	gen `x'borderfe = ""
	gen `x'dim = ""
}

* bXdistance FE
tabulate border, gen(border_)
forvalues i = 1(1)34 {
	gen border_`i'Xx = border_`i'*x
	gen border_`i'Xzx = border_`i'*zx
}

gen index = _n

* programs to store estimates (eststo doesn't work well with cgmboot)
capture program drop myest
program define myest
	args i bw cov borderfe dim
	mat bootresults = e(bootresults)
	svmat bootresults
	local j = 4
	foreach x in z Z Zz {
		replace `x'B = string(_b[`x'], "%20.02fc") if index == `i'
		_pctile bootresults`j' if bootresults3 == 0, nq(1000)
		replace `x'CIL = "[" + string(r(r25), "%20.02fc") + "," if index == `i'
		replace `x'CIU = string(r(r975), "%20.02fc") + "]" if index == `i'
		if `j' == 4 {
			replace `x'N = string(e(N), "%20.00fc") if index == `i'
			replace `x'Ns = string(e(N_fipsstate), "%20.00fc") if index == `i'
			replace `x'bw = "`bw'" if index == `i'
			replace `x'cov = "`cov'" if index == `i' 
			replace `x'rsq = string(e(r2), "%20.02fc") if index == `i'
			replace `x'borderfe = "`borderfe'" if index == `i'
			replace `x'dim = "`dim'" if index == `i'
		}
		local j = `j' + 1
	}	
	drop bootresults*
end

local nreps 500

*** insurance 2014-2013 ***

xi: cgmwildboot dpctui20142013 z Z Zz x zx Zx Zzx i.border `covariates' if abs(x) < 100, cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest 1 "100 miles" "Yes" "Border" "1-D"
xi: cgmwildboot dpctui20142013 z Z Zz x zx Zx Zzx border_* `covariates' if abs(x) < 100, cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest 2 "100 miles" "Yes" "Border X distance" "1-D"
xi: cgmwildboot dpctui20142013 z Z Zz x zx Zx Zzx latit longit latXlon lat2 lon2 border_lon border_lat `covariates', cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest 3 "100 miles" "Yes" "None" "2-D"

*** insurance 2015-2013 ***

xi: cgmwildboot dpctui20152013 z Z Zz x zx Zx Zzx i.border `covariates' if abs(x) < 100, cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest 4 "100 miles" "Yes" "Border" "1-D"
xi: cgmwildboot dpctui20152013 z Z Zz x zx Zx Zzx border_* `covariates' if abs(x) < 100, cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest 5 "100 miles" "Yes" "Border X distance" "1-D"
xi: cgmwildboot dpctui20152013 z Z Zz x zx Zx Zzx latit longit latXlon lat2 lon2 border_lon border_lat `covariates', cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
myest 6 "100 miles" "Yes" "None" "2-D"

* output

preserve

	keep if index < 7
	keep *B *CI* *N *Ns *bw *cov *borderfe *dim *rsq
	l

	sxpose, clear force
	drop if mi(_var1)
	l

	gen rowname = ""
	replace rowname = "Medicaid expansion" if _n == 1
	replace rowname = "High eligibility" if _n == 4
	replace rowname = "Expansion X eligibility" if _n == 7
	replace rowname = "Number of Counties" if _n == 10
	replace rowname = "Number of States" if _n == 11
	replace rowname = "Window" if _n == 12
	replace rowname = "Covariates" if _n == 13
	replace rowname = "R-squared" if _n == 14
	replace rowname = "Fixed effects" if _n == 15
	replace rowname = "Distance" if _n == 16
	order rowname 
	keep if _n <= 19

	#delimit;

	listtex 
		using "_output/tableA7.tex"
		,
		replace
		type rstyle(tabular)
		head(
			\begin{tabular*}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{7}{c}}
			\toprule &
			\multicolumn{3}{c}{\underline{Percent uninsured}} & 
			\multicolumn{3}{c}{\underline{Percent uninsured}} \\
			& \multicolumn{3}{c}{\underline{2014-2013}} &
			\multicolumn{3}{c}{\underline{2015-2013}} \\
			&(1)&(2)&(3)&(4)&(5)&(6)\\
			\midrule
		)
		foot("\bottomrule\end{tabular*}")
		;
		
	#delimit cr

	l

restore
